package com.lry.config;

import com.lry.bean.ExceptionNotice;
import com.lry.properties.EmailProperties;
import com.lry.send.EmailNoticeSendComponent;
import com.lry.send.INoticeSendComponent;
import com.lry.text.NoticeTextResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.MailSender;

@Configuration
@ConditionalOnProperty(value = "ea.email.enable",havingValue = "true",matchIfMissing = true)
@EnableConfigurationProperties(EmailProperties.class)
@Slf4j
public class EaEmailConfig {


    @Bean("emailSendingComponent")
    public INoticeSendComponent<ExceptionNotice> emailNoticeSendComponent(MailProperties mailProperties,
                                                                          MailSender mailSender,
                                                                          EmailProperties emailNoticeProperties) {
        log.debug("创建邮件异常通知");
        INoticeSendComponent<ExceptionNotice> component =
                new EmailNoticeSendComponent(mailSender,mailProperties, emailNoticeProperties, exceptionNoticeTextResolver());
        return component;
    }

    @Bean
    @ConditionalOnMissingBean
    public NoticeTextResolver<ExceptionNotice> exceptionNoticeTextResolver() {
        return x -> x.createText();
    }

}
